package com.cxrj.zhsq.civilianManage.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cxrj.zhsq.civilianManage.entity.Civilian;
import com.cxrj.zhsq.civilianManage.entity.CivilianType;
import com.cxrj.zhsq.civilianManage.entity.LegalService;
import com.cxrj.zhsq.civilianManage.service.CivilianService;

@RestController
@RequestMapping("/civilian")
public class CivilianController {

	@Resource
	private CivilianService civilianService;

	/**
	 * 查询所有数据
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "seleteCivilianPage", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> seleteCivilianPage(HttpServletRequest request, HttpServletResponse response) {
		return civilianService.seleteCivilianPage(request, response);
	}

	/**
	 * 插入数据
	 * 
	 * @param civilian
	 * @return
	 */
	@RequestMapping("insertCivilian")
	public Map<String, Object> insertCivilian(Civilian civilian) {
		return civilianService.insertCivilian(civilian);
	}

	/**
	 * 启用禁用
	 * 
	 * @param civilian
	 * @return
	 */
	@RequestMapping("updateCivilian")
	public Map<String, Object> updateCivilian(Civilian civilian) {
		return civilianService.updateCivilian(civilian);
	}

	@RequestMapping("deleteCivilian")
	public Map<String, Object> deleteCivilian(String code) {
		return civilianService.deleteCivilian(code);
	}

	/**
	 * 查询所有数据
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "seleteCivilianTypePage", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> seleteCivilianTypePage(HttpServletRequest request, HttpServletResponse response) {
		return civilianService.seleteCivilianTypePage(request, response);
	}

	/**
	 * 查询所有数据
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "seleteCivilianAll", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> seleteCivilianAll(HttpServletRequest request) {
		return civilianService.seleteCivilianAll();
	}

	/**
	 * 插入数据
	 * 
	 * @param civilian
	 * @return
	 */
	@RequestMapping("insertCivilianType")
	public Map<String, Object> insertCivilianType(CivilianType civilianType) {
		return civilianService.insertCivilianType(civilianType);
	}

	/**
	 * 启用禁用
	 * 
	 * @param civilian
	 * @return
	 */
	@RequestMapping("updateCivilianType")
	public Map<String, Object> updateCivilianType(CivilianType civilianType) {
		return civilianService.updateCivilianType(civilianType);
	}

	@RequestMapping("deleteCivilianType")
	public Map<String, Object> deleteCivilianType(String id) {
		return civilianService.deleteCivilianType(id);
	}

	/**
	 * 根据编码查询
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "seleteCivilianTypeByCode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> seleteCivilianTypeByCode(String code, HttpServletRequest request) {
		return civilianService.selectByCivilianCode(code, null);
	}

	/**
	 * 查询所有法律服务
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "seleteLegalServicePage", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> seleteLegalServicePage(HttpServletRequest request, HttpServletResponse response) {
		return civilianService.selectLegalServicePage(request, response);
	}

	/**
	 * 添加或修改法律服务
	 * 
	 * @return
	 */
	@RequestMapping(value = "insertAndUpdateLegalService", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> insertAndUpdateLegalService(LegalService legalService, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			if (!legalService.getApprovalTimeStr().equals("")) {
				legalService.setApprovalTime(new SimpleDateFormat("yyyy-MM-dd").parse(legalService.getApprovalTimeStr()));
			}
			resultMap = civilianService.insertAndUpdateLegalService(legalService);
		} catch (Exception e) {
			resultMap.put("result", 0);
			e.printStackTrace();
		}
		return resultMap;
	}

	/**
	 * 根据id查询法律服务
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "seleteLegalServiceById", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
	public Map<String, Object> seleteLegalServiceById(String id, HttpServletRequest request) {
		return civilianService.selectLegalServiceById(id);
	}
	
	/**
	 * 删除法律服务
	 * 
	 * @return
	 */
	@RequestMapping(value = "delLegalService", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delLegalService(String id, HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			resultMap = civilianService.updateLegalService(id);
		} catch (Exception e) {
			resultMap.put("result", 0);
			e.printStackTrace();
		}
		return resultMap;
	}

}
